﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class SaleEndPoints
    {

        private const string SalesUrl = "api/sales";
        private HttpClient client;

        public SaleEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<bool> CreateSaleAsync(SaleViewModel sale, int reservationId)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(SalesUrl +"/"+ reservationId, sale);
            bool success = false;
            if (response.IsSuccessStatusCode)
            {
                success = await response.Content.ReadAsAsync<bool>();
            }
            return success;
        }

        public async Task<int> GetAvailableSeatCount(int screeningId)
        {
            int availableSeatCount = 0;
            HttpResponseMessage response = await client.GetAsync("api/availableSeatCount/" + screeningId);
            if (response.IsSuccessStatusCode)
            {
                availableSeatCount = await response.Content.ReadAsAsync<int>();
            }
            return availableSeatCount;
        }

        public async Task<string> GetOccupiedSeats(int screeningId)
        {
            string occupiedSeats = String.Empty;
            HttpResponseMessage response = await client.GetAsync("api/occupiedSeats/" + screeningId);
            if (response.IsSuccessStatusCode)
            {
                occupiedSeats = await response.Content.ReadAsAsync<string>();
            }
            return occupiedSeats;
        }
    }
}
